import { showToast } from "../../utils/asyncWx"
Page({
  data: {
    imgs: [], // 本地图片地址数组
    picPaths: [], // 网络路径
    isTop: false
  },
  onLoad () {

  },
  // 点击上传图片
  handleUploadImage () {
    let that = this
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#000",
      success (res) {
        if (!res.cancel) {
          if (res.tapIndex === 0) {
            that.chooseWxImage('album')
          } else if (res.tapIndex === 1) {
            that.chooseWxImage('camera')
          }
        }
      }
    })
  },
  // 图片本地路径
  chooseWxImage (type) {
    let that = this
    let imgsPaths = that.data.imgs
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success (res) {
        that.upImgs(res.tempFilePaths[0], 0)
      }
    })
  },
  // 上传服务器
  upImgs (imgUrl, index) {
    let that = this
    wx.uploadFile({
      url: 'https://xxxxxxxxxxx',
      filePath: imgUrl,
      name: 'file',
      header: {
        'content-type': "multipart/form-data"
      },
      formData: null,
      success (res) {
        // 接口返回网络路径
        console.log(res)
        var data = JSON.parse(res.data)
        that.data.picPaths.push(data['msg'])
        that.setData({
          picPaths: that.data.picPaths
        })
        console.log(that.data.picPaths)
      }
    })
  },

  // 置顶
  handleTop () {
    this.setData({
      isTop: !this.data.isTop
    })
    if (this.data.isTop) {
      showToast('置顶成功~')
    } else {
      showToast('取消置顶成功~')
    }
  }
})